package net.mihome.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import net.mihome.app.R;
import net.mihome.app.bean.RssNews;
import net.mihome.app.common.UIHelper;
import net.mihome.app.ui.RssWebViewActivity;

import java.util.List;

public class RssNewsAdapter extends BaseAdapter {
	private List<RssNews> list;
	private Context context;
    static class InnerView{
        TextView textView;
        TextView descTextView;
        TextView timeTextView;
        TextView hrefTextView;
        ImageView imageView;
    }
	public RssNewsAdapter(Context context, List<RssNews> list){
		this.context=context;
		this.list=list;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0).getId();
	}

	/**
     * 下面是重要代码
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

            InnerView listItemView = null;

	        LayoutInflater _LayoutInflater=LayoutInflater.from(this.context);
	        if(convertView==null)
	        {
                convertView=_LayoutInflater.inflate(R.layout.rss_list_news_item, null);
                listItemView=new InnerView();
                listItemView.textView=(TextView) convertView.findViewById(R.id.rss_news_item_title);
                listItemView.descTextView=(TextView) convertView.findViewById(R.id.rss_news_item_desc);
                listItemView.timeTextView=(TextView) convertView.findViewById(R.id.rss_news_item_time);

                listItemView.hrefTextView=(TextView) convertView.findViewById(R.id.rss_news_item_href);
                listItemView.imageView=(ImageView) convertView.findViewById(R.id.news_img);
		        setOnclick(convertView);
                convertView.setTag(listItemView);
	        }else{
                listItemView= (InnerView) convertView.getTag();
            }
        String titleString=list.get(position).getTitle();
        if(titleString!=null){
            titleString= titleString.trim();
        }
        listItemView.textView.setText(titleString);
        String description=list.get(position).getDescription();
        if(description!=null){
            description= description.trim();
        }
        listItemView.descTextView.setText(description);
        listItemView.timeTextView.setText("发布时间:"+list.get(position).getPubDates());
        listItemView.hrefTextView.setText(list.get(position).getHref());
        if(list.get(position).getThumbPic()!=null&&!"".equals(list.get(position).getThumbPic())&&!"null".equals(list.get(position).getThumbPic())){
            listItemView.imageView.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(list.get(position).getThumbPic(),listItemView.imageView);
            listItemView.imageView.setTag(list.get(position).getThumbPic());
            listItemView.imageView.setOnClickListener(imageClickListener);
        }else{
            listItemView.imageView.setVisibility(View.GONE);
        }
        return convertView;
    }
    /**
     * 添加点击时间
     * */
    public void setOnclick(View view){
    	view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View iteView) {
				TextView textView=(TextView) iteView.findViewById(R.id.rss_news_item_href);
				String url=textView.getText().toString();
				Intent intent=new Intent(context,RssWebViewActivity.class);
				intent.putExtra("href",url);
				context.startActivity(intent);
			}
		});
    }
    private View.OnClickListener imageClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            UIHelper.showImageZoomDialog(v.getContext(), (String) v.getTag());
        }
    };
}
